package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.*;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    //加载文章详情 数据回显
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //获取当前登录用户
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //创建对象用于保存返回数据
        ApArticleBehavior apArticleBehavior = new ApArticleBehavior();
        //非空判断
        if (userId == null) {
            //未登录,四种行为直接返回false
            apArticleBehavior.setIsfollow(false);
            apArticleBehavior.setIslike(false);
            apArticleBehavior.setIsunlike(false);
            apArticleBehavior.setIscollection(false);
            return ResponseResult.okResult(apArticleBehavior);
        }
        //登录了,获取行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        //基础查询条件
        Query query = Query.query(Criteria.where("entryId").is(userId.toString()).and("articleId").is(dto.getArticleId()));
        //查询点赞行为
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        //非空判断
        if (apLikesBehavior == null) {
            //没有点赞行为
            apArticleBehavior.setIslike(false);
        } else {
            //有点赞行为
            apArticleBehavior.setIslike(true);
        }
        //查询不喜欢行为
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        //非空判断
        if (apUnlikesBehavior == null) {
            //没有点赞行为
            apArticleBehavior.setIsunlike(false);
        } else {
            //有点赞行为
            apArticleBehavior.setIsunlike(true);
        }
        //查询收藏行为
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        //非空判断
        if (apCollection == null) {
            //没有点赞行为
            apArticleBehavior.setIscollection(false);
        } else {
            //有点赞行为
            apArticleBehavior.setIscollection(true);
        }
        //在redis查询关注行为
        Double score = redisTemplate.opsForZSet().score("apuser:follow:" + userId, dto.getAuthorApUserId().toString());
        //非空判断
        if (score == null) {
            //没有关注作者
            apArticleBehavior.setIsfollow(false);
        } else {
            //关注了作者
            apArticleBehavior.setIsfollow(true);
        }
        //返回结果
        return ResponseResult.okResult(apArticleBehavior);
    }
}
